package com.liujun.io.netty.eight.protobuftest;

import java.util.ArrayList;
import java.util.List;

import com.google.protobuf.InvalidProtocolBufferException;
import com.liujun.io.netty.eight.protobuf.SubscribeReqProto;

public class TestSubscribeReqProto {

	private static byte[] encode(SubscribeReqProto.SubscribeReq req) {
		return req.toByteArray();
	}

	private static SubscribeReqProto.SubscribeReq decode(byte[] body)
			throws InvalidProtocolBufferException {
		return SubscribeReqProto.SubscribeReq.parseFrom(body);
	}

	private static SubscribeReqProto.SubscribeReq createSubscribeReq() {
		SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq
				.newBuilder();
		builder.setSubReqID(1);
		builder.setUserName("liujun");
		builder.setProductName("Netty Book");
		List<String> address = new ArrayList<>();
		address.add("NanJing YuHuaTai");
		address.add("BeiJing liuliChang");
		address.add("BeiJing liuliChang");
		address.add("ShenZhen HongShuLin");
		builder.addAllAddress(address);

		return builder.build();
	}

	public static void main(String[] args)
			throws InvalidProtocolBufferException {

		long start = System.currentTimeMillis();

		for (int i = 0; i < 10000; i++) {
			SubscribeReqProto.SubscribeReq req = createSubscribeReq();
			// System.out.println("Before encode:"+req.toString());
			//byte[] bvalue = encode(req);

			SubscribeReqProto.SubscribeReq req2 = decode(encode(req));
			// System.out.println("After decode : "+req.toString());
			// System.out.println("Assert equal : --> "+ req2.equals(req));
		}

		long end = System.currentTimeMillis();

		System.out.println((end - start)/1000f);

	}

}
